package com.example.allplan.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.allplan.Fragment.JiHuaFragment;
import com.example.allplan.ItemTouchHelper.JiHuaBaoTouchCallback;
import com.example.allplan.JavaBean.JiHuaBaoJavaBean;
import com.example.allplan.JavaBean.JiHuaJavaBean;
import com.example.allplan.MyApplication;
import com.example.allplan.MyView.MyLinearLayout;
import com.example.allplan.R;
import com.example.allplan.RecyclerAdapter.JiHuaBaoRecyclerAdapter;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Collections;

public class GuanLiJiHuaBaoActivity extends BaseActivity {
    MyLinearLayout mgen;
    RecyclerView guan_li_bao_recyclerview;
    ArrayList<JiHuaBaoJavaBean> mlist;
    MyApplication mmyapp;
    JiHuaFragment mmyfragment;
    ImageView bei_jing_tu;
    AppCompatActivity mactivity;
    JiHuaBaoRecyclerAdapter madapter;
    Toolbar mtoolbar;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guan_li_ji_hua_bao_activity);
        init();
    }

    void reload() {
        mlist = mmyapp.mSuoYouJiHuaBaoSQCaoZuo.reloadAllJiHuaBao();
        mlist.remove(0);
        mlist.remove(0);
        Collections.sort(mlist);
    }
    JiHuaBaoJavaBean  reloadonebyid(int n){
       JiHuaBaoJavaBean mo= mmyapp.mSuoYouJiHuaBaoSQCaoZuo.reloadByIdJiHuaBao(n);
        Toast.makeText(mmyapp, mo.getBao_ming(), Toast.LENGTH_SHORT).show();
        return mo;
    }

    void init() {
        mgen=findViewById(R.id.guan_li_ji_hua_bao_gen_mylinearlayout);
        mmyapp = MyApplication.getInstance();
        mtoolbar=findViewById(R.id.guan_li_ji_hua_bao_toolbar);
        setSupportActionBar(mtoolbar);
        bei_jing_tu = findViewById(R.id.guan_li_ji_hua_bao_imageview);
        reload();
        if (mlist.size() == 0) {
            bei_jing_tu.setVisibility(View.VISIBLE);
        }
        Collections.sort(mlist);
        guan_li_bao_recyclerview = findViewById(R.id.guan_li_ji_hua_bao_recyclerview);
        madapter = new JiHuaBaoRecyclerAdapter(mlist, new JiHuaBaoRecyclerAdapter.onItemClickListener() {
            @Override
            public void onClick(int pos, Context mcon) {
                Intent mintent = new Intent(mcon, BianJiYiGeJiHuaBao.class);
                Bundle mbd = new Bundle();
                mbd.putInt("biao_qing_zhi", mlist.get(pos).getBiao_qing_fu_hao_zhi());
                mbd.putInt("id", mlist.get(pos).getId());
                mbd.putInt("pai_xu_you_biao", mlist.get(pos).getPai_xu_you_biao());
                mbd.putInt("lei_xing", mlist.get(pos).getLei_xing());
                mbd.putInt("ji_hua_shu",mlist.get(pos).getJi_hua_shu());
                mbd.putString("biao_ti_ming", mlist.get(pos).getBao_ming());
                mbd.putInt("pos_zhi",pos);
                Toast.makeText(mcon, ""+pos, Toast.LENGTH_SHORT).show();
                mintent.putExtras(mbd);
                mactivity = (AppCompatActivity) mcon;
                mactivity.startActivityForResult(mintent, 21);
            }
        }, this);
        guan_li_bao_recyclerview.setAdapter(madapter);
        guan_li_bao_recyclerview.setLayoutManager(new LinearLayoutManager(this));
        JiHuaBaoTouchCallback mcallback = new JiHuaBaoTouchCallback(new JiHuaBaoTouchCallback.itemtouchhelpercallback() {


            @Override
            public void onitemmove(int frompos, int topos) {

                int temp = mlist.get(frompos).getPai_xu_you_biao();
                mlist.get(frompos).setPai_xu_you_biao(mlist.get(topos).getPai_xu_you_biao());
                mlist.get(topos).setPai_xu_you_biao(temp);
                Collections.swap(mlist, frompos, topos);
                madapter.notifyItemMoved(frompos, topos);
            }

            @Override
            public void onitemswipe(int direction) {
                //Toast.makeText(mmyapp, "" + direction, Toast.LENGTH_SHORT).show();
                mlist.remove(direction);
               // Log.d("kaishi",System.currentTimeMillis()+"");
                for(int i=direction;i<mlist.size();i++){
                    mlist.get(i).setPai_xu_you_biao(mlist.get(i).getPai_xu_you_biao()-1);
                }
                madapter.notifyItemRemoved(direction);
                //Log.d("kaishi1",System.currentTimeMillis()+"");
                mgen.setCantclick(true);
               new Thread(new Runnable() {
                   @Override
                   public void run() {
                       try {
                           Thread.sleep(450);
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                       runOnUiThread(new Runnable() {
                           @Override
                           public void run() {

                               //madapter.notifyDataSetChanged();
                               mgen.setCantclick(false);
                               //Log.d("kaishi2",System.currentTimeMillis()+"");
                           }
                       });
                   }
               }).start();
                //Toast.makeText(mmyapp, "" + mlist.size()+"*"+madapter.getItemCount(), Toast.LENGTH_SHORT).show();
            }
        });
        ItemTouchHelper mhelp = new ItemTouchHelper(mcallback);
        mhelp.attachToRecyclerView(guan_li_bao_recyclerview);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.bian_ji_yi_ge_ji_hua_bao_menu,menu);
        return true;
    }
    void gengaijihuasuoshu(){
        ArrayList<JiHuaJavaBean> msuoyou=new ArrayList<JiHuaJavaBean>();
        msuoyou=mmyapp.mSuoYouJiHuaSQCaoZuo.reloadAllJiHua();
        JiHuaBaoJavaBean mmoren=mmyapp.mSuoYouJiHuaBaoSQCaoZuo.reloadByIdJiHuaBao(0);
        for(int i=0;i<msuoyou.size();i++){
            boolean zhaodao=false;
            for(int j=0;j<mlist.size();j++){

                if(msuoyou.get(i).getSuo_shu_ji_hua_bao_id()==mlist.get(j).getId()){
                    zhaodao=true;
                    break;
                }
            }
            if(!zhaodao&&msuoyou.get(i).getSuo_shu_ji_hua_bao_id()!=0&&msuoyou.get(i).getSuo_shu_ji_hua_bao_id()!=1){
                JiHuaJavaBean temp=msuoyou.get(i);
                temp.setYuan_suo_shu_id(0);
                temp.setSuo_shu_ji_hua_bao_id(0);
                temp.setPai_xu_you_biao(mmoren.getJi_hua_shu());
                mmoren.setJi_hua_shu(mmoren.getJi_hua_shu()+1);
               mmyapp.mSuoYouJiHuaSQCaoZuo.update_one(temp);
            }
        }
        mmyapp.mSuoYouJiHuaBaoSQCaoZuo.update_one(mmoren);
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.bian_ji_yi_ge_ji_hua_bao_wan_cheng:
                setResult(22);
                mmyapp.mSuoYouJiHuaBaoSQCaoZuo.delete_suo_you_chuang_jian();
                mmyapp.putSH_Int(mmyapp.JI_HUA_BAO_SHU_SHARE_NAME,0);
                mmyapp.ji_hua_bao_shu=0;
                for(int i=0;i<mlist.size();i++){
                    mmyapp.mSuoYouJiHuaBaoSQCaoZuo.write_one(mlist.get(i));
                    mmyapp.ji_hua_bao_shu++;
                }
                mmyapp.putSH_Int(mmyapp.JI_HUA_BAO_SHU_SHARE_NAME,mmyapp.ji_hua_bao_shu);
                gengaijihuasuoshu();
                finish();
                return true;
            case android.R.id.home:
                finish();
                return true;

        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 21 && resultCode == 12) {
            int pos =data.getIntExtra("pos_zhi",-1);
            int id=data.getIntExtra("geng_gai_de_id",-1);
            Toast.makeText(mmyapp, ""+pos+"."+id, Toast.LENGTH_SHORT).show();
            mlist.set(pos,reloadonebyid(id));
            madapter.notifyItemChanged(pos);
            setResult(22);
        }
    }
}
